<template>
	<globalModal :title="title" :visible="visible" :confirmLoading="confirmLoading" @ok="handleOk" @cancel="handleCancel">
		<a-spin :spinning="confirmLoading">
			<a-form :form="form" autocomplete="off">
				<a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="上级菜单">
					<a-tree-select
						placeholder="请选择上级菜单"
						:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
						:tree-data="treeData"
						show-search
						:replaceFields="replaceFields"
						@change="changeMenu"
						treeNodeFilterProp="title"
						v-decorator="[
							'parentId',
							{
								rules: [{ required: true, message: '请选择上级菜单' }]
							}
						]"
					/>
				</a-form-item>
				<a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="类型">
					<a-radio-group v-decorator="['type', { initialValue: '0' }]" @change="changeMenutype">
						<a-radio value="0">页面</a-radio>
						<a-radio value="1">模块</a-radio>
						<a-radio value="2">按钮</a-radio>
					</a-radio-group>
				</a-form-item>
				<a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="名称">
					<a-input placeholder="名称" v-decorator="['name', { rules: [{ required: true, message: '名称' }, { max: 50, message: '名称不超过50个字符' }], getValueFromEvent: removeSpaces }]" />
				</a-form-item>

				<a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="权限标识">
					<a-input placeholder="权限标识" v-decorator="['perms', { rules: [{ required: true, message: '权限标识' }], getValueFromEvent: removeSpaces }]" />
				</a-form-item>
				<a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="排序号" extra="数据越小越靠前">
					<a-input-number v-decorator="['orderNum', { initialValue: 1 }]" :min="1" style="width: 100%;" />
				</a-form-item>
			</a-form>
		</a-spin>
	</globalModal>
</template>

<script>
import { sysManageapi } from '@/api/index.js';
import { ModalMixins } from '@/mixins';
export default {
	name: 'minimenuListModal',
	mixins: [ModalMixins],
	data() {
		return {
			permissions: [],
			menutype: 0,
			linkway: 2,
			isAddRoot: false,
			iconChooseVisible: false,
			treeData: [],
			replaceFields: { children: 'children', title: 'name', key: 'menuId', value: 'menuId' }
		};
	},
	props: {
		MenuTree: {
			type: Array,
			default: () => []
		}
	},
	components: {},
	created() {},
	methods: {
		//打开弹框初始事件
		getDefaultList() {
			this.getMenuTree();
			this.menutype = this.mdl.type || '0';
		},
		//新增数据回填
		setAddFieldsValue() {
			if (this.mdl.parentId) {
				this.form.setFieldsValue(this.$pick(this.mdl, 'parentId'));
			}
		},
		//编辑数据回填
		// setFieldsValue() {
		// 	this.form.setFieldsValue(this.$pick(this.mdl, 'title', 'perms', 'type', 'orderNum', 'parentId'));
		// },
		//提交保存数据
		handleOk() {
			// 触发表单验证
			this.form.validateFields((err, values) => {
				// 验证表单没错误
				if (!err) {
					/* 处理地址 */
					let { menuId } = this.mdl;
					let parameter = Object.assign({}, values, this.mdl.menuId ? { menuId: this.mdl.menuId } : '');
					this.saveInfo(this.isAdd ? this.api.add : this.api.edit, parameter);
				}
			});
		},
		getMenuTree() {
			console.log('MenuTree', this.MenuTree);
			this.treeData = [
				{
					menuId: '0',
					name: '根目录',
					children: [...this.MenuTree]
				}
			];
		},
		changeMenu(value) {
			console.log('value', value);
		},
		changeMenutype(e) {
			this.menutype = e.target.value;
		},
		chooseIcon(icon) {
			this.iconChooseVisible = false;
			this.form.setFieldsValue({
				icon: icon
			});
		},
		selectIcons() {
			this.iconChooseVisible = true;
		}
	}
};
</script>

<style scoped></style>
